/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.musiclibrary.backend.dao;

import cz.muni.fi.pa165.musiclibrary.api.dao.SongDAO;
import cz.muni.fi.pa165.musiclibrary.api.entities.IllegalEntityException;
import cz.muni.fi.pa165.musiclibrary.api.entities.Song;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author xbogar
 */
@Repository
public class SongDAOImpl implements SongDAO{

    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    
    public void createSong(Song song){
        if(song == null){
              throw new IllegalArgumentException("song is null");
        }
        if(song.getId() != null){
            throw new IllegalEntityException();
        }
        if(song.getName()==null){
            throw new IllegalArgumentException("song name is null");
        }
        if(song.getAuthorId()==null){
            throw new IllegalArgumentException("author id is null");
        }
        if(song.getAlbumId()==null){
            throw new IllegalArgumentException("album id is null");
        }
        entityManager.persist(song);


    }

    public Song getSong(Long id) {
        if(id == null){
              throw new IllegalArgumentException("id is null");
        }
        Song song = entityManager.find(Song.class, id);
        return song;
    }
    
    public List<Song> getAllSongs() {
        Query query = entityManager.createQuery(
                "SELECT song FROM Song song");
        List<Song> listOfSongs = (List<Song>) query.getResultList();
        return listOfSongs;
    }

    public void updateSong(Song song) {
        if(song == null || song.getId() == null){
              throw new IllegalArgumentException("song is null");
        }
        Song found = entityManager.find(Song.class, song.getId());
        if(found == null || found.getId() != song.getId()){
            throw new IllegalEntityException("no such song in database.");
        }
        
        if(song.getName()==null){
            throw new IllegalArgumentException("song name is null");
        }
        if(song.getAuthorId()==null){
            throw new IllegalArgumentException("author id is null");
        }
        if(song.getAlbumId()==null){
            throw new IllegalArgumentException("album id is null");
        }
        
        if (!found.getAlbumId().equals(song.getAlbumId())) {
            found.setAlbumId(song.getAlbumId());
        }
        if (!found.getAuthorId().equals(song.getAuthorId())) {
            found.setAuthorId(song.getAuthorId());
        }
        if (!found.getGenreId().equals(song.getGenreId())) {
            found.setGenreId(song.getGenreId());
        }
        if (found.getBitrate() != song.getBitrate()) {
            found.setBitrate(song.getBitrate());
        }
        if (!found.getComment().equals(song.getComment())) {
            found.setComment(song.getComment());
        }
        if (!found.getName().equals(song.getName())) {
            found.setName(song.getName());
        }
        if (found.getSongOrder() != song.getSongOrder()) {
            found.setSongOrder(song.getSongOrder());
        }

        entityManager.persist(found);

    }

    public void deleteSong(Song song) {
        if(song == null){
            throw new IllegalArgumentException("song is null");
        }
        if(song.getId() == null){
            throw new IllegalArgumentException();
        }
        Song found = entityManager.find(Song.class, song.getId());
        if(found == null){
            throw new IllegalEntityException("no such song in database.");
        }
        if (!songCompare(song, found)){
            throw new IllegalEntityException();
        }

        entityManager.remove(found);

    }
    
    private static boolean songCompare(Song s1, Song s2){
        s1.equals(s2);
        boolean e1 = s1.getName().equals(s2.getName());
        boolean e2 = s1.getAlbumId().equals(s2.getAlbumId());
        boolean e3 = s1.getAuthorId().equals(s2.getAuthorId());
        boolean e4 = s1.getBitrate() == s2.getBitrate();
        boolean e5 = s1.getComment().equals(s2.getComment());
        boolean e6 = s1.getGenreId().equals(s2.getGenreId());
        boolean e7 = s1.getSongOrder() == s2.getSongOrder();
        return (e1 && e2 && e3 && e4 && e5 && e6 && e7);
    }
    
}
